package model.admin;

import java.sql.ResultSet;

import myutil.Connect;


import vo.DepartmentDetailsVo;

public class ModifyDepartmentsMo {

	public Boolean updateDepartmentDetails(String department_name,String desc,int dep_id,String type){
	String query="update department_details" +
			     "   set department_name	=	'"+department_name+"' ," +
			     "  	 description		=	'"+desc+"' " +
			     " where department_id		=	"+dep_id+";";
		Connect c = new Connect();
		System.out.println("query is \t"+query+" \n");
		try{
			c.dml(query);
			c.logger(type, "Update");
		return true;
		}
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());e.printStackTrace();return false;
		}
		
	}
	public void deleteDepartmentDetails(int batch_id, int department_id,String type){
		String query="update degree_department  " +
				     "   set status	=	0 " +
					 " where department_id 	=	"+department_id+" " +
					 "   and batch_id 		=	"+batch_id+";";
		if (batch_id==0)
			query="update department_details  " +
				     "   set status	=	'Inactive' " +
					 " where department_id 	=	"+department_id+";";
		Connect c = new Connect();
		System.out.println(query);
		try{
			c.dml(query); 
			c.logger(type, "Update");
		}
		catch (Exception e) {
			// TODO: handle exception
		System.out.println(e.getMessage());
		e.printStackTrace();
		
		}
	}
	
	public void updateDepartmentDegree(int department_id,int degree_id,String type){
		String query="",query1="",query2="";
		String descr="";
		Connect c= new Connect();
		query1="select description from department_details where department_id="+department_id;
		query2="select description from degree_details where degree_id="+degree_id;
		
		
		try {
			ResultSet rs= c.getResult(query1);
			while(rs.next()){
				descr=rs.getString("description");
			}
			rs=c.getResult(query2);
			while(rs.next()){
				descr=descr+rs.getString("description");
			}
			query="insert into degree_department_mgmt(department_id,degree_id,description,status) values ("+department_id+" , "+ degree_id+" , '"+descr+"' , 'Active')";
			System.out.println(query);
			c.dml(query);
			c.logger(type, "Insert");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateDepartmentBatch(int department_id,int batch_id,String type){
		String query="";
		Connect c= new Connect();
		query="insert into degree_department(department_id,batch_id,status) values("+department_id+" ,"+batch_id +" ,'Active')";
		System.out.println(query);
		try {
			c.dml(query);
			c.logger(type, "Insert");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void updateDepartmentProfessor(int department_id, int staff_id,String type){
		String query="";
		Connect c= new Connect();
		query="insert into professor_department(professor_id,department_id,position,role,status) values("+staff_id+","+department_id+",(select designation from college_staff where staff_id="+staff_id+"),(select role from college_staff where staff_id="+staff_id+" ),'Active')";
		System.out.println(query);
		try {
			c.dml(query);
			c.logger(type, "Insert");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void updateDepartmentCourse(int department_id, int course_id,String type){
		
		String query="";
		Connect c= new Connect();
		query="update course_details set department_id="+department_id+" where course_id="+course_id;
		System.out.println(query);
		try {
			c.dml(query);
			c.logger(type, "Update");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean addBatchDepartmentDetails(String department_name,
			String description, int batch_id, int start_year,String type) {
		String query = "";
		query="INSERT INTO degree_department (" +
				"department_id," +
				"degree_id," +
				"batch_id, " +
				"start_year, " +
				"description, " +
				"status)" +
				" VALUES " +
				"((select department_id from department_details where department_name='"+department_name+"')," +
				" (select degree_id from degree_management where batch_id=" + batch_id+"),"+
				batch_id+",'"+
				start_year+"'," +
				"'"+description+"'," +
				"'Active');";
		Connect c = new Connect();
		System.out.println(query);
		try{
			c.dml(query);
			c.logger(type, "Insert");
			return true;
			}
		catch (Exception e) {
			// TODO: handle exception
		System.out.println(e.getMessage());
		e.printStackTrace();
		return false;
		}
	}
		public Boolean addNewDepartment(DepartmentDetailsVo department,String type) {
			String query = "INSERT INTO department_details (" + "department_name, "
					+ "start_year, " + "description, " + "status) VALUES ('"
					+ department.getDepartment_name() + "', '"
					+ department.getStart_year() + "', '"
					+ department.getDescription() + "', '" + "1');";
			Connect c = new Connect();
			try {
				c.dml(query);
				c.logger(type, "Insert");
				return true;
			} catch (Exception e) {
				// TODO: handle exception
				return false;
			}
		}
	}
